package com.dcivision.framework.taglib.channel;

import java.sql.Connection;
import java.util.List;

import javax.servlet.jsp.tagext.BodyTagSupport;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.workflow.bean.MtmWorkflowProgressUserRecord;
import com.dcivision.workflow.bean.WorkflowProgress;
import com.dcivision.workflow.core.WorkflowRetrievalManager;
import com.dcivision.workflow.dao.MtmWorkflowProgressUserRecordDAObject;

public class AjaxFromWhomInfoTag extends BodyTagSupport {

  private WorkflowProgress workflowProgress = null;
  private WorkflowRetrievalManager retrievalManager = null;
  private MtmWorkflowProgressUserRecordDAObject progressUserDAO =null;
  
  public String getAjaxContent() throws ApplicationException{
    this.init();
    return getContent();
  }
  
  public String getContent() throws ApplicationException{
    
    StringBuffer content = new StringBuffer();
    List wkprogressList = retrievalManager.getNonSystemParent((workflowProgress).getTrackID(),(workflowProgress).getWorkflowStepID());
    StringBuffer sbcomment = new StringBuffer();
    
    if ( wkprogressList.size() == 0 ) {
        sbcomment.append ("<nobr><b>" );
      sbcomment.append ( MessageResourcesFactory.getMessage(((SessionContainer)pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY)).getSessionLocale(), "workflow.label.step_status_"+ workflowProgress.getStatus()) );
        sbcomment.append ( " at <b>"+ TextUtility.formatTimestamp((workflowProgress).getCreateDate())+"</b></nobr>");
    } else {
        for(int j=0;j<wkprogressList.size();j++){
          WorkflowProgress wkflowprogress = (WorkflowProgress)wkprogressList.get(j);
          sbcomment.append ( "<b><u style=\'font-size:11px\'>" );
          sbcomment.append ( retrievalManager.getStepNameByWkProgressID(wkflowprogress.getID()) );
          sbcomment.append ( "</u></b><br>" );
          List progressUserList = retrievalManager.getWorkflowProgressUserRecordListByProgressID(wkflowprogress.getID());
            for (int k=0; k<progressUserList.size(); k++) {
          MtmWorkflowProgressUserRecord progressUser = (MtmWorkflowProgressUserRecord) progressUserList.get(k);
          sbcomment.append ("<nobr>" );
          sbcomment.append ("<b>" + progressUser.getUpdaterName()+"</b> at <b>");
          sbcomment.append ( TextUtility.formatTimestamp(progressUser.getUpdateDate())+"</b>");
          if (!Utility.isEmpty(progressUser.getReason())) {
              sbcomment.append (":<br>&nbsp;&nbsp;<i>"+progressUser.getReason()+"</i>");
          }
              sbcomment.append ("</nobr>" );
           }
         }
    }
    String comment = sbcomment.toString();
    List userActorList = progressUserDAO.getListByWorkflowProgressID( (workflowProgress).getID(),null,WorkflowProgress.STATUS_IN_PROGRESS);
    if (!Utility.isEmpty( userActorList )) {    
      for (int j=0; j<userActorList.size(); j++) {
        MtmWorkflowProgressUserRecord wfProgressUserRecord = (MtmWorkflowProgressUserRecord)userActorList.get(j);
        content.append("<a  "+makeMailToInfo( wfProgressUserRecord)+" onmouseover=\"return getPopupInfo('"+TextUtility.escapeJSString(TextUtility.br(comment))+"');\" onmouseout=\"return nd();\">"+wfProgressUserRecord.getUpdaterName()+"</a><br>");
      }
    }else {
      content.append("<a  "+makeMailToInfo()+" onmouseover=\"return getPopupInfo('"+TextUtility.escapeJSString(TextUtility.br(comment))+"');\" onmouseout=\"return nd();\">");
      content.append((workflowProgress).getCreatorName());
      content.append("</a>");
      }
    return content.toString();
  }
  private String makeMailToInfo(MtmWorkflowProgressUserRecord wfProgressUserRecord) {
    String mailTo = "";
    if (!Utility.isEmpty(UserInfoFactory.getUserEmailAddress(wfProgressUserRecord.getUpdaterID()))) {
      mailTo = " class=\"stepname\" href=\"mailto:" + UserInfoFactory.getUserEmailAddress(wfProgressUserRecord.getUpdaterID()) + "\"";
    }else{
      mailTo = " style=\"text-decoration: none\"  ";
    }
    return mailTo;
  }
  private String makeMailToInfo() {
    String mailTo = "";
    if (!Utility.isEmpty(UserInfoFactory.getUserEmailAddress((workflowProgress).getCreatorID()))) {
      mailTo = " class=\"stepname\" href=\"mailto:" + UserInfoFactory.getUserEmailAddress((workflowProgress).getCreatorID()) + "\"";
    }else{
      mailTo = " style=\"text-decoration: none\" ";
    }
    return mailTo;
  }
  public void release(){
    workflowProgress = null;
    super.release();
  }

  private void init(){
    if(retrievalManager==null){
    Connection conn = (Connection)pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
    SessionContainer sessionContainer = (SessionContainer)pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    retrievalManager = new WorkflowRetrievalManager(sessionContainer,conn);
     progressUserDAO = new MtmWorkflowProgressUserRecordDAObject(sessionContainer,conn);
    }
  }
  public WorkflowProgress getWorkflowProgress() {
    return workflowProgress;
  }

  public void setWorkflowProgress(WorkflowProgress workflowProgress) {
    this.workflowProgress = workflowProgress;
  }
  
}
